import 'package:flutter/material.dart';
import 'package:bear_story/views.dart';
import '../main.dart';
import 'user_set.dart';

class SettingsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _SettingsPageState();
  }
}

class _SettingsPageState extends State<SettingsPage> {
  Widget _getImgWidget() {
    final user = getNowUser();
    if (user.headUrl == null || user.headUrl.length == 0) {
      return Container(
        width: 28,
        height: 28,
        child: ClipOval(
          child: Image.asset(
            "images/mrtx.png",
            fit: BoxFit.fill,
          ),
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(14)), border: Border.all(color: Color(0xffcccccc), width: 1)),
      );
    } else {
      return Container(
        width: 28,
        height: 28,
        child: ClipOval(
          child: Image.network(
            user.headUrl,
            fit: BoxFit.fill,
          ),
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(14)), border: Border.all(color: Color(0xffcccccc), width: 1)),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          title: Text("设置"),
          textTheme: TextTheme(title: TextStyle(color: Color(0xff000000), fontSize: 19)),
          backgroundColor: Colors.white,
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
          iconTheme: IconThemeData(color: Color(0xff000000))
//        leading: Icon(Icons.arrow_back,color: Color(0xff000000),size: 18,semanticLabel: "ffe",),
          ),
      body: Container(
        constraints: BoxConstraints.expand(),
        color: background_color,
        child: Column(
          children: <Widget>[
            Padding(padding: EdgeInsets.only(top: 14)),
            TitleItem(
              "头像",
              img: _getImgWidget(),
              onTap: () {
                pushPage(context, UserHeadUrlSetPage());
              },
            ),
            TitleItem(
              "呢称",
              content: getNowUser().nickName,
              onTap: () {
                pushPage(context, UserNickNameSetPage());
              },
            ),
            TitleItem(
              "性别",
              content: getNowUser().getSexStr(),
              onTap: () {
                pushPage(context, UserSexSetPage());
              },
            ),
            TitleItem(
              "年龄",
              content: "${getNowUser().age}周岁",
              onTap: () {
                pushPage(context, UserAgeSetPage());
              },
            ),
            TitleItem(
              "手机号码",
              content: getNowUser().phoneNumber,
            ),
            Padding(
              padding: EdgeInsets.all(15),
              child: Container(
                constraints: BoxConstraints.expand(height: 44),
                child: FlatButton(
                  onPressed: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return AlertDialog(
                            content: Text("退出登录？"),
                            actions: <Widget>[
                              FlatButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                  child: Text("取消")),
                              FlatButton(
                                  onPressed: () {
                                    updateNowUser(null);
                                    Navigator.of(context).pop();
                                    Navigator.of(context).pop("logout");
                                  },
                                  child: Text("确定")),
                            ],
                          );
                        });
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(25))),
                  color: Color(0xffff4d4d),
                  child: Text(
                    "退出登录",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
